#include "TurnNeckToAuxBehaviour.h"
#include "../CommonFunctions.h"

/*
 *	Otoci krkom, otocenie je mozne korigovat.
 *
 *	@param correction	korekcia otocenia
 */
void TurnNeckToAuxBehaviour::Behave(float correction)
{
	// Kam treba krk natocit
	int rel = f2int(CalcDir(DATA_STORAGE.turnNeck.futurePosition - DATA_STORAGE.turnNeck.futureMyPosition, 
		world->me->GetAbsDir() + DegAngle(DATA_STORAGE.turnNeck.nextTurn)).GetDeg() + correction);
	if(rel < world->params->server->minneckang)
	{
		rel = f2int(world->params->server->minneckang);
	}
	else if(rel > world->params->server->maxneckang)
	{
		rel = int(world->params->server->maxneckang);
	}

	int turn = f2int(rel - world->me->GetNeckDir().GetDeg());
			
	if(turn > world->params->server->maxneckmoment)
	{
		turn = int(world->params->server->maxneckmoment);
	}
	else if(turn < world->params->server->minneckmoment)
	{
		turn = f2int(world->params->server->minneckmoment);
	}

	commandGenerator->TurnNeck(turn);
	DATA_STORAGE.turnNeck.timeOfLastTurnNeck = world->state->GetSimTime();
}
